#ifndef _PINGER_HPP_
#define _PINGER_HPP_

#include "cinatra/use_asio.hpp"
#include <asio.hpp>
#include <asio/steady_timer.hpp>

#include <istream>
#include <iostream>
#include <ostream>
#include <chrono>

#include "icmp_header.hpp"
#include "ipv4_header.h"

#ifdef WIN32
#define  BOOST_ASIO_WINDOWS
#endif // WIN32



using boost::asio::ip::icmp;

class pinger
{
public:
	pinger(asio::io_service& io_service,const char* destination);
	~pinger();
	int state_;
private:
	void start_send();
	void start_receive();
	void handle_receive(asio::error_code code, std::size_t length);
	
	void stop_steady_timer();

	uint16_t get_identifier();

	void handle_timeout(boost::system::error_code const& ec);
	
private:
	

	icmp::resolver resolver_;
	icmp::endpoint destination_;
	icmp::socket socket_;

	uint16_t sequence_number_;

	boost::asio::streambuf reply_buffer_;
	std::size_t num_replies_;

	boost::asio::steady_timer timer_;
	bool enable_timeout_ = true;

	std::string msg;
	std::string dest_ip_;
	std::chrono::steady_clock::time_point time_sent_;
	asio::io_context& io_context_;
	
};

#endif // _PINGER_H_